import { defineStore } from 'pinia'
import { getApi as fetchMenus } from '@/api/views/system/menu'
import type { RouteRecordRaw, } from 'vue-router'
import { dealTreeData } from '@/utils'
export const useRouteStore = defineStore('routes', {
  state: () => ({ routes: [] }),
  persist: {
    enabled: true,
    strategies: [
      {
        storage: sessionStorage,
        paths: ['routes'],
      },
    ],
  },
  getters: {
    getRoutes: state => state.routes,
  },
  actions: {
    async fetchMenuList({ router, callback }: any) {
      const res = await fetchMenus({})
      const ret: any = res
      if (ret.code === 200) {
        dealTreeData(ret.data).forEach((route: RouteRecordRaw) => {
          router.addRoute(route)
        })
        this.routes = router.getRoutes()
        callback && callback()
      }
    },
  },
})